home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / delphi.swg / 0178_Some general methods to control Windows.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-08-30  |  4.9 KB  |  217 lines

  1. (*
  2.   DESCRIPTION A simple component with some methods to control windows
  3.   AUTHOR      Harm van Zoest, email 4923559@hsu1.fnt.hvu.nl
  4.   VERSION     0.95 (beta), 07-05-96
  5.   REMARK      If you have comments, found bugs or you add some interestig new features,
  6.               please mail me!
  7. *)
  8.  
  9. unit WinUtil;
  10.  
  11. interface
  12.  
  13. uses
  14.   Classes, ExtCtrls;
  15.  
  16. type
  17.   TWinUtil = class(TComponent)
  18.   private
  19.     FTimer: TTimer;
  20.     Expired: Boolean;
  21.     procedure Expire(Sender: TObject);
  22.     function GetInterval: LongInt;
  23.     procedure SetInterval(AInterval: LongInt);
  24.     procedure Sleep;
  25.   public
  26.     constructor Create(AOwner: TComponent); override;
  27.     destructor Destroy; override;
  28.     procedure Restart;
  29.     procedure Reboot;
  30.     procedure ShutDown;
  31.     procedure CopyFile( source, dest : string);
  32.     procedure SleepFor(AInterval: LongInt);
  33.     function GetEnvironvar(const VariableName: string): string;
  34.     function GetWindir: string;
  35.     function GetCompanyName: string;
  36.     function GetUserName : string;
  37.   published
  38.     property Interval: LongInt read GetInterval write SetInterval;
  39.   end;
  40.  
  41. procedure Register;
  42.  
  43. implementation
  44.  
  45. uses
  46.   WinTypes, WinProcs,LZexpand, sysutils,Forms;
  47.  
  48. constructor TWinUtil.Create(AOwner: TComponent);
  49. begin
  50.   inherited Create(AOwner);
  51.   FTimer := TTimer.Create(Self);
  52.   FTimer.Enabled := False;
  53. end;
  54.  
  55. destructor TWinUtil.Destroy;
  56. begin
  57.   FTimer.Free;
  58.   FTimer := nil;
  59.   inherited Destroy;
  60. end;
  61.  
  62. procedure TWinUtil.Expire(Sender: TObject);
  63. begin
  64.    Expired := True;
  65. end;
  66.  
  67. function TWinUtil.GetInterval: LongInt;
  68. begin
  69. if Assigned(FTimer) then
  70.   Result := FTimer.Interval
  71. else Result := 0;
  72. end;
  73.  
  74. procedure TWinUtil.SetInterval(AInterval: LongInt);
  75. begin
  76.   if Assigned(FTimer) then
  77.     FTimer.Interval := AInterval;
  78. end;
  79.  
  80. procedure TWinUtil.Sleep;
  81. begin
  82.   if Assigned(FTimer) then
  83.   begin
  84.     Expired := False;
  85.     FTimer.OnTimer := Expire;
  86.     FTimer.Enabled := True;
  87.   repeat
  88.       Application.ProcessMessages;
  89.   until Expired;
  90.   FTimer.Enabled := False;
  91.   end;
  92. end;
  93.  
  94. procedure TWinUtil.SleepFor(AInterval: LongInt);
  95. begin
  96.   if Assigned(FTimer) then
  97.   begin
  98.     if FTimer.Interval <> AInterval then
  99.       FTimer.Interval := AInterval;
  100.     Sleep;
  101.     end;
  102. end;
  103.  
  104. function TWinUtil.GetEnvironVar(const VariableName: string): string;
  105. var
  106.   APChar, VPChar: PChar;
  107. begin
  108.   GetMem(VPChar, Length(VariableName) + 1);
  109.   { place the pascal-style string in a null-terminated one}
  110.   StrPCopy(VPChar, VariableName);
  111.   APChar:=GetDOSEnvironment;
  112.   while not ((APChar^ = #1) or
  113.              (StrLIComp(APChar, VPChar, (StrScan(APChar, '=') - APChar)) = 0)) do
  114.        Inc(APChar, StrLen(APChar) + 1);
  115.   FreeMem(VPChar, Length(VariableName) + 1);
  116.   if APChar^ = #1 then
  117.     Result:=''
  118.   else
  119.     Result:=Copy(StrPas(APChar), (StrScan(APChar, '=') - APChar) + 2, 255);
  120. end;{GetEnviron}
  121.  
  122.  
  123. { get the windows dir}
  124. function TWinUtil.GetWindir: string;
  125. var
  126.   x : word;
  127.   buf : Pchar;
  128. begin
  129.   { get memory}
  130.   Getmem(buf , 500);
  131.   { call api funtion}
  132.   x := GetWindowsDirectory(buf,500);
  133.   GetWindir := StrPas(buf);
  134.   Freemem(buf,500);
  135. end;{GetWindir}
  136.  
  137.  
  138.  
  139. procedure TWinUtil.Restart;
  140. var
  141.   rc : boolean;
  142. begin
  143.   rc := ExitWindows(ew_restartwindows, 0);
  144. end;
  145.  
  146. procedure TWinUtil.Reboot;
  147. var
  148.   rc : boolean;
  149. begin
  150.   rc := ExitWindows(ew_rebootsystem, 0);
  151. end;
  152.  
  153. procedure TWinUtil.Shutdown;
  154. var
  155.   rc : boolean;
  156. begin
  157.   rc := ExitWindows(0, 0);
  158. end;
  159.  
  160. procedure TWinUtil.CopyFile( source, dest : string);
  161. var
  162.   fil : Pchar;
  163.   HandleSource, HandleDest : integer;
  164.   rec : TOFStruct;
  165.   x : longint;
  166. begin
  167.   { get the handle voor de source file}
  168.   Getmem(fil, (length(source)+1));
  169.   strPcopy(fil, source);
  170.   { get the handle which identifies the source file}
  171.   HandleSource := LZOpenfile(fil,rec, OF_READWRITE);
  172.   FreeMem(fil,length(source)+1);
  173.   { create a desination file}
  174.   Getmem(fil, (length(dest)+1));
  175.   strPcopy(fil, dest);
  176.   _lcreat(fil, 0);
  177.   { get the handle which identifies the destination file}
  178.   HandleDest := LZOpenfile(fil, rec, OF_READWRITE);
  179.   { now, we are ready to copy the file}
  180.   x:= LZCopy(HandleSource, HandleDest);
  181.   Freemem(fil,( length(dest) +1));
  182. end;
  183.  
  184. function TWinUtil.GetUserName: string;
  185. var
  186.   fileHandle : Thandle ;
  187.   fileBuffer: Array [0..29] of Char;
  188. begin
  189.   fileHandle := LoadLibrary('USER');
  190.   if fileHandle >= HINSTANCE_ERROR then begin
  191.        If LoadString(fileHandle, 514, @fileBuffer, 30) <> 0 Then
  192.       GetUserName := fileBuffer;
  193.   FreeLibrary(fileHandle);
  194.   end;{if}
  195. end;{GetUserName}
  196.  
  197. function TWinUtil.GetCompanyName: string;
  198. var
  199.   fileHandle : Thandle;
  200.   fileBuffer: Array [0..29] of Char;
  201. begin
  202.   fileHandle := LoadLibrary('USER');
  203.   if fileHandle >= HINSTANCE_ERROR then begin
  204.        If LoadString(fileHandle, 515, @fileBuffer, 30) <> 0 Then
  205.       GetCompanyName := fileBuffer;
  206.   FreeLibrary(fileHandle);
  207.   end;{if}
  208. end;{GetCompanyName}
  209.  
  210.  
  211. procedure Register;
  212. begin
  213. RegisterComponents('System', [TWinUtil]);
  214. end;
  215.  
  216. end.
  217.